﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace ForumPages
{
	/// <summary>
	/// An empty page that can be used on its own or navigated to within a Frame.
	/// </summary>
	public sealed partial class ForumHomePage : Page
	{
		ForumPost post;
		ForumPost modifiedPost;
		List<ForumPost> posts = new List<ForumPost>();
		readonly List<OutputData> datas = new List<OutputData>();

		public ForumHomePage()
		{
			this.InitializeComponent();
			post = new ForumPost()
			{
				Id = 1,
				UserId = 1,
				Title = "Title",
				Content = "Content",
				CreateTime = DateTime.Now,
				UpdateTime = DateTime.Now
			};
			modifiedPost = new ForumPost()
			{
				Id = 1,
				UserId = 2,
				Title = "ModifiedTitle",
				Content = "ModifiedContent",
				CreateTime = DateTime.Now,
				UpdateTime = DateTime.Now
			};
			datas.Add(new OutputData("Test", "Test content"));
			Refresh();
		}

		private void Refresh()
		{
			OutputListView.ItemsSource = null;
			OutputListView.ItemsSource = datas;
			PostListView.ItemsSource = null;
			PostListView.ItemsSource = posts;
		}

		private void CreateBtn_Click(object sender, RoutedEventArgs e)
		{
			try
			{
				HttpUtils utils = new HttpUtils("https://localhost:44300");
				var msg = utils.SendPostMessage("api/ForumPost/CreatePost", post);
				var task = msg.Content.ReadAsStringAsync();
				task.Wait();
				datas.Add(new OutputData(msg.StatusCode.ToString(), task.Result));
			}
			catch (Exception ex)
			{
				datas.Add(new OutputData("Fail", ex.ToString()));
			}
			Refresh();
		}

		private void UpdateBtn_Click(object sender, RoutedEventArgs e)
		{
			try
			{
				HttpUtils utils = new HttpUtils("https://localhost:44300");
				modifiedPost.Title = TitleBox.Text;
				modifiedPost.Content = ContentBox.Text;
				var msg = utils.SendPutMessage("api/ForumPost/UpdatePost", modifiedPost);
				var task = msg.Content.ReadAsStringAsync();
				task.Wait();
				datas.Add(new OutputData(msg.StatusCode.ToString(), task.Result));
			}
			catch (Exception ex)
			{
				datas.Add(new OutputData("Fail", ex.ToString()));
			}
			Refresh();
		}

		private void DeleteBtn_Click(object sender, RoutedEventArgs e)
		{
			try
			{
				HttpUtils utils = new HttpUtils("https://localhost:44300");
				var msg = utils.SendDeleteMessage("api/ForumPost/DeletePost?id=1");
				var task = msg.Content.ReadAsStringAsync();
				task.Wait();
				datas.Add(new OutputData(msg.StatusCode.ToString(), task.Result));
			}
			catch (Exception ex)
			{
				datas.Add(new OutputData("Fail", ex.ToString()));
			}
			Refresh();
		}

		private void RequestBtn_Click(object sender, RoutedEventArgs e)
		{
			try
			{
				HttpUtils utils = new HttpUtils("https://localhost:44300");
				var msg = utils.SendGetMessage("api/ForumPost/GetAllPosts");
				var task = msg.Content.ReadAsStringAsync();
				task.Wait();
				posts = JsonConvert.DeserializeObject<IEnumerable<ForumPost>>(task.Result).ToList();
				datas.Add(new OutputData(msg.StatusCode.ToString(), task.Result));
			}
			catch (Exception ex)
			{
				datas.Add(new OutputData("Fail", ex.ToString()));
			}
			Refresh();
		}

		private void RequestByIdBtn_Click(object sender, RoutedEventArgs e)
		{
			try
			{
				HttpUtils utils = new HttpUtils("https://localhost:44300");
				var msg = utils.SendGetMessage("api/ForumPost/GetPostById?id=1");
				var task = msg.Content.ReadAsStringAsync();
				task.Wait();
				datas.Add(new OutputData(msg.StatusCode.ToString(), task.Result));
			}
			catch (Exception ex)
			{
				datas.Add(new OutputData("Fail", ex.ToString()));
			}
			Refresh();
		}
	}

	public class OutputData
	{
		public string Title { get; set; }
		public string Content { get; set; }

		public OutputData(string title, string content)
		{
			Title = title;
			Content = content;
		}
	}
}
